package de.herberlin.webapp.image.action;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.EntityManager;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.util.Log;
import net.sourceforge.stripes.validation.Validate;
import de.herberlin.webapp.core.AbstractAction;
import de.herberlin.webapp.db.Operation;
import de.herberlin.webapp.db.Service;
import de.herberlin.webapp.image.Gallery;
import de.herberlin.webapp.image.GalleryItem;

@UrlBinding(value = "/admin/SortGallery.action")
public class SortGalleryAction extends AbstractAction {
	private static final long serialVersionUID = 1L;

	private int imageId = -1;

	private String direction = null;

	private String pageId = null;

	public SortGalleryAction() {
	}

	@Validate(required = true, on = { "up", "down", "delete" })
	public void setDirection(String direction) {
		this.direction = direction;
	}

	@Validate(required = true)
	public void setImageId(int imageId) {
		this.imageId = imageId;
	}

	@Validate(required = true)
	public void setPageId(String pageId) {
		this.pageId = pageId;
	}

	@DefaultHandler
	public Resolution perform() {

		logger.debug("imageId=" + imageId + ", direction=" + direction);

		GetGalleryAction op = new GetGalleryAction(pageId);
		Service.perform(op);
		Gallery gallery = op.getGallery();
		
		LinkedHashMap<Integer,GalleryItem> orderedItems=order(gallery.getGalleryItems());
		if ("delete".equals(direction)) {
			GalleryItem item=orderedItems.remove(imageId);
			Service.remove(item);
		} else if ("up".equals(direction)) {
			GalleryItem item=orderedItems.get(imageId);
			item.setPosition(Math.max(0,item.getPosition()-3));
		} else if ("down".equals(direction)) {
			GalleryItem item=orderedItems.get(imageId);
			item.setPosition(item.getPosition()+3);
		} else {
			logger.warn("Unknown action: "+direction);
		}
		
		gallery.setGalleryItems(new LinkedList<GalleryItem> (orderedItems.values()));
		Service.perform(new StoreGalleryOperation(gallery));
		getContext().getRequest().setAttribute("action", op);
		return new RedirectResolution("/admin/gallery.jsp?pageId=" + pageId);

	}

	private LinkedHashMap<Integer,GalleryItem> order(List<GalleryItem> items) {

		LinkedHashMap<Integer,GalleryItem> result=new LinkedHashMap<Integer,GalleryItem>();
		if (items == null) {
			return result;
		}
		int count = 0;
		for (GalleryItem g : items) {
			count+=2;
			g.setPosition(count);
			result.put(g.getId(), g);
		}
		return result;
	}

	
	public static class StoreGalleryOperation implements Operation{
		
		private Gallery gallery=null;
		public StoreGalleryOperation(Gallery g) {
			this.gallery=g;
		}
		public void perform(EntityManager em) throws Exception {
			if (gallery==null || gallery.getGalleryItems()==null) {
				Log.getInstance(getClass()).warn("Gallery must not be null!");
			}
			
			for (GalleryItem g : gallery.getGalleryItems()) {
				em.persist(g);
			}
		}
		
	}
}
